package com.hotelManagerSystem.controller;


import com.hotelManagerSystem.mapper.RoleMapper;
import com.hotelManagerSystem.mapper.StaffMapper;
import com.hotelManagerSystem.model.Role;
import com.hotelManagerSystem.model.Staff;
import com.hotelManagerSystem.model.StaffRole;
import com.hotelManagerSystem.service.IRoleService;
import com.hotelManagerSystem.service.IStaffRoleService;
import com.hotelManagerSystem.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  职工——角色前端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/staff-role")
public class StaffRoleController {
    @Autowired
    private IStaffService iStaffService;
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IStaffRoleService iStaffRoleService;

//    职工角色修改页面跳转
    @RequestMapping("/FPRoleUI")
    public String FPRoleUI(Model model,String staffIdStr){
//        System.out.println("------------FPRoleUI---------------");
        int staffId = Integer.parseInt(staffIdStr);
        Staff staff_db = iStaffService.getById(staffId);
        List<Role> role_dbList = iRoleService.list();
        model.addAttribute("staff_db",staff_db);
        model.addAttribute("role_dbList",role_dbList);

        return "";
    }

//    职工角色修改
    @RequestMapping("/FPRole")
    public String FPRole(Model model, StaffRole staffRole, String staffIdStr, String RoleIdStr){
//        System.out.println("------------FPRole---------------");
        int staffId = Integer.parseInt(staffIdStr);
        int roleId = Integer.parseInt(RoleIdStr);
//        查找数据库是否有这个职工的角色信息，有就刪
        boolean m = iStaffRoleService.deleteStaffRoleByStaffId(staffId);
//        boolean m1 = iStaffRoleService.deleteStaffRoleByStaffId(staffRole.getStaffId());
//        装配staff-role
        staffRole.setRoleId(roleId);
        staffRole.setStaffId(staffId);

        boolean b = iStaffRoleService.saveOrUpdate(staffRole);

//        跳转回职工列表页面
//        return list(model);

        List<Staff> staff_dbList = iStaffService.list();
        model.addAttribute("staff_dbList",staff_dbList);

        return "";
    }
}

